home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / VisualTest.java < prev    next >
Text File  |  1998-09-22  |  13KB  |  436 lines

  1. /*
  2.  * @(#)VisualTest.java    1.13 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. import java.awt.*;
  15. import java.applet.*;
  16.  
  17. class VTest extends Frame {
  18.     boolean    inReshape = false;
  19.     Menu    componentMenu;
  20.     Menu    backgroundMenu;
  21.     Menu    foregroundMenu;
  22.     Menu    shapeMenu;
  23.     Menu    sizeMenu;
  24.     Menu    fontMenu;
  25.     Menu    enableMenu;
  26.     Menu    familyMenu;
  27.     Menu    containerMenu;
  28.  
  29.     int        currentSize = 10;
  30.     Font    currentFont;
  31.     boolean    enableComponents = true;
  32.     Color    currentForeground;
  33.     Color    currentBackground;
  34.     Container    currentContainer;
  35.     Component    component;
  36.     Font    font10;
  37.     Font    font14;
  38.     Font    font24;
  39.     Font    font36;
  40.  
  41.     public VTest() {
  42.     super("VTest");
  43.  
  44.     MenuBar mb = new MenuBar();
  45.     currentContainer = this;
  46.     componentMenu = new Menu("Component");
  47.     componentMenu.add(new MenuItem("Button"));
  48.     componentMenu.add(new MenuItem("Checkbox"));
  49.     componentMenu.add(new MenuItem("Choice"));
  50.     componentMenu.add(new MenuItem("Label"));
  51.     componentMenu.add(new MenuItem("List"));
  52.     componentMenu.add(new MenuItem("Panel"));
  53.     componentMenu.add(new MenuItem("TextArea"));
  54.     componentMenu.add(new MenuItem("TextField"));
  55.     componentMenu.add(new MenuItem("HScrollbar"));
  56.     componentMenu.add(new MenuItem("VScrollbar"));
  57.     mb.add(componentMenu);
  58.  
  59.     enableMenu = new Menu("Enable/Disable");
  60.     enableMenu.add(new MenuItem("Enable"));
  61.     enableMenu.add(new MenuItem("Disable"));
  62.     mb.add(enableMenu);
  63.  
  64.     fontMenu = new Menu("Font");
  65.     familyMenu = new Menu("Family");
  66.     familyMenu.add(new MenuItem("Courier"));
  67.     familyMenu.add(new MenuItem("Dialog"));
  68.     familyMenu.add(new MenuItem("TimesRoman"));
  69.     familyMenu.add(new MenuItem("Helvetica"));
  70.     familyMenu.add(new MenuItem("Symbol"));
  71.     fontMenu.add(familyMenu);
  72.  
  73.     sizeMenu = new Menu("Size");
  74.     sizeMenu.add(new MenuItem("10"));
  75.     font10 = new Font("Helvetica", Font.PLAIN, 10);
  76.     sizeMenu.add(new MenuItem("14"));
  77.     font14 = new Font("Helvetica", Font.PLAIN, 14);
  78.     sizeMenu.add(new MenuItem("24"));
  79.     font24 = new Font("Helvetica", Font.PLAIN, 24);
  80.     sizeMenu.add(new MenuItem("36"));
  81.     font36 = new Font("Helvetica", Font.PLAIN, 36);
  82.     fontMenu.add(sizeMenu);
  83.  
  84.     mb.add(fontMenu);
  85.  
  86.     shapeMenu = new Menu("Move/Reshape");
  87.     shapeMenu.add(new CheckboxMenuItem("Move"));
  88.     shapeMenu.add(new CheckboxMenuItem("Reshape"));
  89.     mb.add(shapeMenu);
  90.  
  91.     foregroundMenu = new Menu("Foreground");
  92.     foregroundMenu.add(new CheckboxMenuItem("default"));
  93.     foregroundMenu.add(new CheckboxMenuItem("red"));
  94.     foregroundMenu.add(new CheckboxMenuItem("green"));
  95.     foregroundMenu.add(new CheckboxMenuItem("blue"));
  96.     mb.add(foregroundMenu);
  97.  
  98.     backgroundMenu = new Menu("Background");
  99.     backgroundMenu.add(new CheckboxMenuItem("default"));
  100.     backgroundMenu.add(new CheckboxMenuItem("red"));
  101.     backgroundMenu.add(new CheckboxMenuItem("green"));
  102.     backgroundMenu.add(new CheckboxMenuItem("blue"));
  103.     mb.add(backgroundMenu);
  104.  
  105.     containerMenu = new Menu("Container");
  106.     containerMenu.add(new MenuItem("FlowLayout"));
  107.     containerMenu.add(new MenuItem("GridLayout02"));
  108.     containerMenu.add(new MenuItem("GridLayout20"));
  109.     containerMenu.add(new MenuItem("GridLayout03"));
  110.     containerMenu.add(new MenuItem("GridLayout30"));
  111.     containerMenu.add(new MenuItem("BorderLayout"));
  112.     mb.add(containerMenu);
  113.  
  114.     setMenuBar(mb);
  115.     setLayout(null);
  116.  
  117.     currentFont = font10;
  118.     currentForeground = getForeground();
  119.     currentBackground = getBackground();
  120.     enableComponents = true;
  121.     resize(500, 300);
  122.     show();
  123.     }
  124.  
  125.     public boolean handleEvent(Event e) {
  126.     switch (e.id) {
  127.       case Event.WINDOW_DESTROY:
  128.         System.exit(0);
  129.         return true;
  130.       case Event.MOUSE_DOWN:
  131.         currentContainer = this;
  132.         setCurrentComponent(e.x, e.y);
  133.         /* fall into next case */
  134.       case Event.MOUSE_DRAG:
  135.         if (component != null) {
  136.         if (inReshape) {
  137.             Rectangle bounds = component.bounds();
  138.             component.resize(Math.abs(e.x-bounds.x), Math.abs(e.y-bounds.y));
  139.             component.validate();
  140.         } else {
  141.             component.move(e.x, e.y);
  142.         }
  143.         }
  144.         return true;
  145.       case Event.MOUSE_UP:
  146.         currentContainer.validate();
  147.         return true;
  148.       default:
  149.         return super.handleEvent(e);
  150.     }
  151.     }
  152.  
  153.     void setAttributes(Component c) {
  154.     if (c instanceof Container) {
  155.         return;
  156.     }
  157.     c.setForeground(currentForeground);
  158.     c.setBackground(currentBackground);
  159.     c.setFont(currentFont);
  160.     if (enableComponents) {
  161.         c.enable();
  162.     } else {
  163.         c.disable();
  164.     }
  165.     }
  166.  
  167.     int computeDistance(int x, int y, Rectangle r) {
  168.     int mx;
  169.     int my;
  170.  
  171.     mx = x - (r.x + (r.width / 2));
  172.     my = y - (r.y + (r.height / 2));
  173.  
  174.     return (mx*mx) + (my*my);
  175.     }
  176.  
  177.     void setCurrentComponent(int x, int y) {
  178.     int n = countComponents();
  179.     int distance = -1;
  180.  
  181.     for (int i=0; i<n; i++) {
  182.         Component c = getComponent(i);
  183.         Rectangle b = c.bounds();
  184.         int       d;
  185.         
  186.         d = computeDistance(x, y, b);
  187.         if (distance == -1 || d < distance) {
  188.         distance = d;
  189.         component = c;
  190.         }
  191.     }
  192.     }
  193.  
  194.     void setAttributes() {
  195.     int n = countComponents();
  196.  
  197.     for (int i=0; i < n; i++) {
  198.         setAttributes(getComponent(i));
  199.     }
  200.     }
  201.  
  202.     public boolean action(Event e, Object arg) {
  203.     if (e.target instanceof MenuItem) {
  204.         Menu menu = (Menu)(((MenuItem)e.target).getParent());
  205.         String label = (String)arg;
  206.  
  207.         if (menu == backgroundMenu) {
  208.         if (label.equals("red")) {
  209.             currentBackground = Color.red;
  210.         } else if (label.equals("green")) {
  211.             currentBackground = Color.green;
  212.         } else if (label.equals("blue")) {
  213.             currentBackground = Color.blue;
  214.         } else if (label.equals("default")) {
  215.             currentBackground = Color.lightGray;
  216.         }
  217.         } else if (menu == foregroundMenu) {
  218.         if (label.equals("red")) {
  219.             currentForeground = Color.red.darker();
  220.         } else if (label.equals("green")) {
  221.             currentForeground = Color.green.darker();
  222.         } else if (label.equals("blue")) {
  223.             currentForeground = Color.blue.darker();
  224.         } else if (label.equals("default")) {
  225.             currentForeground = Color.black;
  226.         }
  227.         } else if (menu == shapeMenu) {
  228.         if (label.equals("Move")) {
  229.             inReshape = false;
  230.         } else if (label.equals("Reshape")) {
  231.             inReshape = true;
  232.         }
  233.         } else if (menu == sizeMenu) {
  234.         if (label.equals("10")) {
  235.             currentFont = font10;
  236.         } else if (label.equals("14")) {
  237.             currentFont = font14;
  238.         } else if (label.equals("24")) {
  239.             currentFont = font24;
  240.         } else if (label.equals("36")) {
  241.             currentFont = font36;
  242.         }
  243.         } else if (menu == familyMenu) {
  244.         font10 = new Font(label, Font.PLAIN, 10);
  245.         font14 = new Font(label, Font.PLAIN, 14);
  246.         font24 = new Font(label, Font.PLAIN, 24);
  247.         font36 = new Font(label, Font.PLAIN, 36);
  248.         switch (currentSize) {
  249.           case 10:
  250.           default:
  251.             currentFont = font10;
  252.             break;
  253.           case 14:
  254.             currentFont = font14;
  255.             break;
  256.           case 24:
  257.             currentFont = font24;
  258.             break;
  259.           case 36:
  260.             currentFont = font36;
  261.             break;
  262.         }
  263.         } else if (menu == enableMenu) {
  264.         if (label.equals("Enable")) {
  265.             enableComponents = true;
  266.         } else if (label.equals("Disable")) {
  267.             enableComponents = false;
  268.         }
  269.         } else if (menu == componentMenu) {
  270.         Component component;
  271.  
  272.         if (label.equalsIgnoreCase("Button")) {
  273.             component = new Button("Button");
  274.         } else if (label.equalsIgnoreCase("Label")) {
  275.             component = new Label("label");
  276.         } else if (label.equalsIgnoreCase("TextField")) {
  277.             component = new TextField("textfield");
  278.         } else if (label.equalsIgnoreCase("Choice")) {
  279.             component = new Choice();
  280.             ((Choice)component).addItem("Choice");
  281.         } else if (label.equalsIgnoreCase("List")) {
  282.             component = new List(4, false);
  283.             ((List)component).addItem("List1");
  284.             ((List)component).addItem("List2");
  285.             ((List)component).addItem("List3");
  286.             ((List)component).addItem("List4");
  287.             ((List)component).addItem("List5");
  288.             currentContainer.add(component);
  289.         } else if (label.equalsIgnoreCase("TextArea")) {
  290.             component = new TextArea(5, 15);
  291.             ((TextArea)component).setText("TextArea");
  292.         } else if (label.equalsIgnoreCase("Checkbox")) {
  293.             component = new Checkbox("Checkbox");
  294.         } else if (label.equalsIgnoreCase("Panel")) {
  295.             component = new VPanel(this);
  296.         } else if (label.equalsIgnoreCase("HScrollbar")) {
  297.             component = new Scrollbar(Scrollbar.HORIZONTAL);
  298.         } else if (label.equalsIgnoreCase("VScrollbar")) {
  299.             component = new Scrollbar(Scrollbar.VERTICAL);
  300.         } else {
  301.             component = new Button("Button");
  302.         }
  303.         if (! (component instanceof Container)) {
  304.             Dimension d = component.preferredSize();
  305.             component.reshape(10, 10, d.width, d.height);
  306.         }
  307.         currentContainer.add(component);
  308.         currentContainer.validate();
  309.         } else if (menu == containerMenu) {
  310.         if (currentContainer != this) {
  311.             if (label.equalsIgnoreCase("FlowLayout")) {
  312.             currentContainer.setLayout(new FlowLayout());
  313.             } else if (label.equalsIgnoreCase("GridLayout02")) {
  314.             currentContainer.setLayout(new GridLayout(0,2));
  315.             } else if (label.equalsIgnoreCase("GridLayout20")) {
  316.             currentContainer.setLayout(new GridLayout(2,0));
  317.             } else if (label.equalsIgnoreCase("GridLayout03")) {
  318.             currentContainer.setLayout(new GridLayout(0,3));
  319.             } else if (label.equalsIgnoreCase("GridLayout30")) {
  320.             currentContainer.setLayout(new GridLayout(3, 0));
  321.             } else if (label.equalsIgnoreCase("BorderLayout")) {
  322.             currentContainer.setLayout(new BorderLayout());
  323.             Component comp1;
  324.             Component comp2;
  325.             Component comp3;
  326.             Component comp4;
  327.             Component comp5;
  328.             switch (currentContainer.countComponents()) {
  329.               case 1:
  330.                 comp1 = currentContainer.getComponent(0);
  331.                 currentContainer.remove(comp1);
  332.                 currentContainer.add("Center", comp1);
  333.                 break;
  334.               case 2:
  335.                 comp1 = currentContainer.getComponent(0);
  336.                 comp2 = currentContainer.getComponent(1);
  337.  
  338.                 currentContainer.remove(comp1);
  339.                 currentContainer.remove(comp2);
  340.  
  341.                 currentContainer.add("North", comp1);
  342.                 currentContainer.add("Center", comp2);
  343.                 break;
  344.               case 3:
  345.                 comp1 = currentContainer.getComponent(0);
  346.                 comp2 = currentContainer.getComponent(1);
  347.                 comp3 = currentContainer.getComponent(2);
  348.                 currentContainer.remove(comp1);
  349.                 currentContainer.remove(comp2);
  350.                 currentContainer.remove(comp3);
  351.  
  352.                 currentContainer.add("North", comp1);
  353.                 currentContainer.add("South", comp2);
  354.                 currentContainer.add("Center", comp3);
  355.                 break;
  356.               case 4:
  357.                 comp1 = currentContainer.getComponent(0);
  358.                 comp2 = currentContainer.getComponent(1);
  359.                 comp3 = currentContainer.getComponent(2);
  360.                 comp4 = currentContainer.getComponent(3);
  361.  
  362.                 currentContainer.remove(comp1);
  363.                 currentContainer.remove(comp2);
  364.                 currentContainer.remove(comp3);
  365.                 currentContainer.remove(comp4);
  366.  
  367.                 currentContainer.add("North", comp1);
  368.                 currentContainer.add("South", comp2);
  369.                 currentContainer.add("East", comp3);
  370.                 currentContainer.add("Center", comp4);
  371.                 break;
  372.               case 5:
  373.               default:
  374.                 comp1 = currentContainer.getComponent(0);
  375.                 comp2 = currentContainer.getComponent(1);
  376.                 comp3 = currentContainer.getComponent(2);
  377.                 comp4 = currentContainer.getComponent(3);
  378.                 comp5 = currentContainer.getComponent(4);
  379.  
  380.                 currentContainer.remove(comp1);
  381.                 currentContainer.remove(comp2);
  382.                 currentContainer.remove(comp3);
  383.                 currentContainer.remove(comp4);
  384.                 currentContainer.remove(comp5);
  385.  
  386.                 currentContainer.add("North", comp1);
  387.                 currentContainer.add("South", comp2);
  388.                 currentContainer.add("East", comp3);
  389.                 currentContainer.add("West", comp4);
  390.                 currentContainer.add("Center", comp5);
  391.                 break;
  392.             }
  393.             }
  394.             currentContainer.validate();
  395.         }
  396.         }
  397.         setAttributes();
  398.         return true;
  399.     }
  400.     return false;
  401.     }
  402. }
  403.  
  404. public class VisualTest extends Applet {
  405.     public void init() {
  406.     new VTest();
  407.     }
  408.  
  409.     public static void main(String args[]) {
  410.     Frame f = new Frame("VisualTest");
  411.     VisualTest visualtest = new VisualTest();
  412.  
  413.     visualtest.init();
  414.     visualtest.start();
  415.  
  416.     f.add("Center", visualtest);
  417.     }
  418. }
  419.  
  420. class VPanel extends Panel {
  421.     VTest    target;
  422.  
  423.     public VPanel(VTest target) {
  424.     this.target = target;
  425.     setBackground(target.getBackground().darker());
  426.     resize(100, 100);
  427.     }
  428.  
  429.     public boolean mouseDown(Event evt, int x, int y) {
  430.     target.currentContainer = this;
  431.     target.containerMenu.enable();
  432.     return true;
  433.     }
  434. }
  435.  
  436.